package com.demo.springdatajpademo.controller;

import com.demo.springdatajpademo.entity.TUser;
import com.demo.springdatajpademo.repositroy.UserRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class UserController {
    @Resource
    private UserRepository userRepository;

    @GetMapping("/user/id/{id}")
    public Object findById(@PathVariable Integer id) {
        return userRepository.findById(id).get();
    }

    @GetMapping("/user")
    public TUser saveUser(TUser user) {
        return userRepository.save(user);
    }

    @GetMapping("/user/name/{username}")
    public TUser findByUsername(@PathVariable String username) {
        //需要自己拼接 % % 来实现模糊查找
        return userRepository.findTUserByUsernameLike("%"+username+"%");
    }
}
